package com.chendx.store.user.controller;

import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.user.pojo.Address;
import com.chendx.store.api.user.AddressControllerApi;
import com.chendx.store.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by chendx on 2022/4/19.
 */
@RestController
@RequestMapping("/api/user/address")
public class AddressController implements AddressControllerApi{

    @Autowired
    private AddressService addressService;

    @Override
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Address address) {
        return addressService.add(address);
    }

    @Override
    @GetMapping("/one")
    public ResponseResult findById(Integer id) {
        Address address = addressService.getById(id);
        return ResponseResult.okResult(address);
    }

    @Override
    @GetMapping("/all")
    public ResponseResult findAll() {
        return ResponseResult.okResult(addressService.list());
    }

    @Override
    @GetMapping("/loaduser")
    public ResponseResult userAddress() {
        return addressService.userAddress();
    }

    @Override
    @GetMapping("/default")
    public ResponseResult defaultAddress() {
        return addressService.defaultAddress();
    }

    @Override
    @GetMapping("/setDefault/{id}")
    public ResponseResult setDefaultAddress(@PathVariable Integer id) {
        return addressService.setDefaultAddress(id);
    }

    @Override
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        boolean b = addressService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
